package com.alkaid.anime.controller;

import com.alkaid.anime.service.CommentService;
import com.alkaid.commons.controller.BaseController;
import com.alkaid.commons.entity.Comment;
import com.alkaid.commons.param.anime.AnimeCommentGroup;
import com.alkaid.commons.utils.JsonResult;
import com.alkaid.commons.utils.JwtUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @author Kylin
 */
@RestController
@RequestMapping("api/anime")
public class CommentController extends BaseController {

    private final CommentService commentService;

    public CommentController(CommentService commentService) {
        this.commentService = commentService;
    }

    @GetMapping("/comment/{animeId}")
    public JsonResult<List<Comment>> getThumb(@PathVariable Integer animeId) {
        List<Comment> commentList = commentService.getComments(animeId);
        return new JsonResult<>(200, commentList, "success");
    }

    /**
     * 发表评论
     *
     * @param request the request
     * @param comment the comment
     * @return the json result
     */
    @PostMapping("/comment")
    public JsonResult<Void> addComment(HttpServletRequest request, @Validated(AnimeCommentGroup.class) Comment comment) {
        String jwt = request.getHeader("Authorization");
        JwtUtils jwtUtils = new JwtUtils();
        jwtUtils.parseJwt(jwt);
        Long userId = jwtUtils.getUser().getId();
        comment.setUserId(userId);
        commentService.addComment(comment);
        return new JsonResult<>(200, null, "发表评论成功");
    }
}
